package com.gc.server.controller.api;

import com.gc.server.entity.BookEntity;
import com.gc.server.entity.BookTypeEntity;
import com.gc.server.service.BookService;
import com.gc.server.service.BookTypeService;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/book")
public class ApiBookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private BookTypeService typeService;

    @GetMapping("/index")
    @IgnoreAuth
    public R index(Integer type){
        List<BookEntity> books = bookService.findByType(type);
        List<BookTypeEntity> types = typeService.findByIdx();
        return R.ok(books).putExtra("types",types);
    }

    @PostMapping("/vote")
    public R vote(@LoginUserId Long userId, Long id){
        bookService.vote(userId,id);
        return R.ok();
    }

}
